package com.gemme.gemme.login;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.gemme.gemme.MainActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.manager.DataManager;

import org.json.JSONObject;

import java.util.HashMap;

public class BindInputTelActivity extends AppCompatActivity {
   Context context =  BindInputTelActivity.this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_input_tel);
        judge();
        getData();
        initView();



        requestPemission();
    }
    String weixin_openid;
    String weixin_accessToken;
    String account;
    int type;
    private void getData() {
        try {

            weixin_openid = getIntent().getStringExtra("weixin_openid");

            weixin_accessToken = getIntent().getStringExtra("weixin_accessToken");

            account = getIntent().getStringExtra("account");

            type = getIntent().getIntExtra(MsgActivity.TYPE,MsgActivity.type_tel);


        }catch (Exception e){


        }
    }
    EditText acount;
    EditText et_yqm_code;
    View ll_yqm_code;
    private void initView() {

        acount = findViewById(R.id.acount);

        et_yqm_code = findViewById(R.id.et_yqm_code);

        ll_yqm_code = findViewById(R.id.ll_yqm_code);
        Button button1 = findViewById(R.id.button1);

        if(!TextUtils.isEmpty(account)){
            acount.setText(account + "");
        }

        if(type == MsgActivity.type_tel){
            ll_yqm_code.setVisibility(View.VISIBLE);
        }else if(type == MsgActivity.type_weixin){
//                isbindingmobileExist();
        }else if(type == MsgActivity.type_weibo){

        }

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(ll_yqm_code.getVisibility() == View.VISIBLE){
                    toLogin();
                }else{
                    isbindingmobileExist();
                }


            }
        });
    }

    private void toLogin() {
        String tel = acount.getText() + "";
        String yqm_code = et_yqm_code.getText() + "";
        if (TextUtils.isEmpty(tel)){
            Toast.makeText(context, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return;

        }

        if(type == MsgActivity.type_tel){
            Intent intent = new Intent(context, MsgActivity.class);
            intent.putExtra("account",tel);
            intent.putExtra("yqm_code",yqm_code);
//                    intent.putExtra("weixin_openid",weixin_openid);
//                    intent.putExtra("weixin_accessToken",weixin_accessToken);

            intent.putExtra(MsgActivity.TYPE,MsgActivity.type_tel);
            startActivity(intent);
        }else if(type == MsgActivity.type_weixin){
            Intent intent = new Intent(context, MsgActivity.class);
            intent.putExtra("account",tel);
            intent.putExtra("yqm_code",yqm_code);
            intent.putExtra("weixin_openid",weixin_openid);
            intent.putExtra("weixin_accessToken",weixin_accessToken);

            intent.putExtra(MsgActivity.TYPE,MsgActivity.type_weixin);
            startActivity(intent);
        }else if(type == MsgActivity.type_weibo){

        }


//                getMsg(tel);
    }

    //检测手机号是否存在
    private void isbindingmobileExist() {
        String account = acount.getText() + "";
        if(TextUtils.isEmpty(account)){
            Toast.makeText(context,"手机号不能为空",Toast.LENGTH_SHORT);
            return;
        }
        if(TextUtils.isEmpty(account))
            return;
        //http://192.168.1.6:9999/user/userapp/isbindingmobile?openId=35_-NVQprUOyqmdeyoGa8CR-uE4TEneDd5rCYwt6vSvbtjc5Dp5ZjlFvHIlvjiYD1ToE4qbKy18Fdyo4t5CWrx_g5X-K2tOrG7RPPG1S6T2ulQ
        String url = DataManager.BaseUrl + "user/userapp/isbindingmobile?" +
                "openId=" + account;
        ;
        HashMap<String, String> headMap = new HashMap<>();
        DataManager.toServer3(context, url, DataManager.POST_TYPE, "", null, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    JSONObject jsonObject = new JSONObject(response);
                    boolean isbindingmobile = jsonObject.getBoolean("data");
                    System.out.println("");

                    if(isbindingmobile){//已绑定获取用户信息
                        //有账号流程
                       /* Intent intent = new Intent(context, MsgActivity.class);
                        intent.putExtra("account",account);
                        intent.putExtra(MsgActivity.TYPE,MsgActivity.type_tel);
                        context.startActivity(intent);*/
                        ll_yqm_code.setVisibility(View.GONE);
                        toLogin();
                    }else{//未注册,填写手机号,邀请码
                       /* Intent intent = new Intent(context, BindInputTelActivity.class);
                        intent.putExtra("weixin_openid",weixin_openid);
                        intent.putExtra("weixin_accessToken",weixin_accessToken);
                        intent.putExtra("account",account);
                        intent.putExtra(MsgActivity.TYPE,MsgActivity.type_tel);
                        context.startActivity(intent);*/
                        Toast.makeText(context, "新用户可以填写邀请码", Toast.LENGTH_SHORT).show();
                        ll_yqm_code.setVisibility(View.VISIBLE);
                    }
                }catch (Exception e){

                }
            }
        });
    }

    private void getMsg(String tel) {
//        http://192.168.1.6:9999/user/userapp/bindingsms

        String url = DataManager.BaseUrl + "user/userapp/bindingsms?" +
                "smsType=" + "binding_wx"
                +"&mobile=" + tel;
        ;
        HashMap<String, String> headMap = new HashMap<>();
        DataManager.toServer2(context, url, DataManager.POST_TYPE, "", null, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try{
                    JSONObject jsonObject = new JSONObject(response);
                    boolean bindingsms = jsonObject.getBoolean("data");
                    System.out.println("");

                    if(bindingsms){//获取验证码成功
                        System.out.println("");
                    }
                }catch (Exception e){

                }
            }
        });
    }

    private void requestPemission() {
        if(Build.VERSION.SDK_INT>=23){
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CALL_PHONE,Manifest.permission.READ_LOGS,Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.SET_DEBUG_APP,Manifest.permission.SYSTEM_ALERT_WINDOW,Manifest.permission.GET_ACCOUNTS,Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this,mPermissionList,123);
        }
    }


    private void judge() {
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
         String access_token = MSP.getString(Constant.ACCESS_TOKEN, "").trim();

        if(!TextUtils.isEmpty(access_token)){
            startActivity(new Intent(context, MainActivity.class));
            finish();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }
}